package com.cqeec.gams.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cqeec.gams.common.Function;
import com.cqeec.gams.dao.AccountMapper;
import com.cqeec.gams.dao.AccountRoleMapper;
import com.cqeec.gams.dao.FunctionalModuleMapper;
import com.cqeec.gams.entity.Account;
import com.cqeec.gams.entity.AccountRole;
import com.cqeec.gams.entity.FunctionalModule;

/**
 * 账户服务实现。
 * 
 * @author 唐礼飞
 *
 */
@Service
public class AccountServiceImpl implements AccountService {
	@Autowired
	private AccountMapper accountMapper;
	@Autowired
	private AccountRoleMapper arMapper;
	@Autowired
	private FunctionalModuleMapper fmMapper;

	@Override
	public long count() {
		return accountMapper.count();
	}

	@Override
	public List<Account> findAll() {
		return accountMapper.findAll();
	}

	@Override
	public Account findById(String id) {
		return accountMapper.findById(id);
	}

	@Override
	public void insert(Account entity) {
		if (entity.getId() == null) {
			entity.setId(UUID.randomUUID().toString());
		}
		accountMapper.insert(entity);
	}

	@Override
	public void update(Account entity) {
		accountMapper.update(entity);
	}

	@Override
	public void delete(String id) {
		accountMapper.delete(id);
	}

	@Override
	public Account login(String name, String password) {
		return accountMapper.findByNameAndPassword(name, password);
	}

	@Override
	public Map<String, Object> updatePassword(String id, String oldPassword, String newPassword,
			String confirmPassword) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 新密码与确认密码一致性验证
		if (!newPassword.equals(confirmPassword)) {
			map.put("code", 1);
			map.put("message", "新密码与确认密码不一致！");
			return map;
		}
		// 验证用户原密码
		Account account = accountMapper.findById(id);
		if (!account.getPassword().equals(oldPassword)) {
			map.put("code", 2);
			map.put("message", "用户原密码错误！");
			return map;
		}
		// 修改密码
		account = new Account();
		account.setId(id);
		account.setPassword(newPassword);
		try {
			accountMapper.update(account);
			map.put("code", 0);
			map.put("message", "用户密码修改成功！");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 3);
			map.put("message", "用户密码修改失败！");
		}
		return map;
	}

	@Override
	public List<Account> findPage(int page, int size) {
		long startRow = (page - 1) * size;
		return accountMapper.findPage(startRow, size);
	}

	@Override
	public void insertAccountRole(AccountRole ar) {
		arMapper.insert(ar);
	}

	@Override
	public List<AccountRole> getAccountRole(String id) {
		return arMapper.findByAccount(id);
	}

	@Override
	public void deleteAccountRole(String id) {
		arMapper.deleteAccountRole(id);
	}

	@Override
	public List<Function> getFunction(String id) {
		List<Function> list = new ArrayList<Function>();
		// 查找所有功能
		List<FunctionalModule> functionModuleList = fmMapper.findByAccount(id);
		// 用于临时存储功能组，方便后移除
		List<FunctionalModule> tempList = new ArrayList<FunctionalModule>();
		// 封装功能组
		Function function;
		for (FunctionalModule fm : functionModuleList) {
			if ("1".equals(fm.getType())) {
				function = new Function();
				function.setFunctionGroup(fm);
				list.add(function);
				tempList.add(fm);
			}
		}
		// 移除功能组
		functionModuleList.removeAll(tempList);
		tempList.clear();
		// 封装功能组的功能模块
		String functionGroupId;
		for (Function f : list) {
			functionGroupId = f.getFunctionGroup().getId();
			for (FunctionalModule fm : functionModuleList) {
				if (functionGroupId.equals(fm.getParent())) {
					f.addFunctionModule(fm);
					tempList.add(fm);
				}
			}
			functionModuleList.removeAll(tempList);
			tempList.clear();
		}
		return list;
	}

}
